package ex.com.jm.food.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 获取当前系统的时间
 * @author zhaojw
 *
 */
public class DateUtils {
	//先获取当前系统的时间年-月-日 时:分:秒
	public  static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
	//public  static Calendar  cal   =   Calendar.getInstance();
	/**
	 * 获取当前时间
	 * @return
	 */
	public static String getCurrentDate(){
		//将时间转换为String类型
		String currDate=df.format(new Date());// new Date()为获取当前系统时间
		return currDate;
	}
	/**
	 * 获取昨天时间
	 * @return
	 */
	public static String getYesterday(){
		Calendar  cal   =   Calendar.getInstance();
		cal.add(Calendar.DATE, -1);
		String yesterday=df.format(cal.getTime());
		return yesterday;
		
	}
	/**
	 * 获取最近7天时间
	 * @return
	 */
	public static String get7Days(){
		Calendar  cal   =   Calendar.getInstance();
		cal.add(Calendar.DATE, -7);
		String day7=df.format(cal.getTime());
		return day7;
	}
	/**
	 * 获取最近15时间
	 * @return
	 */
	public static String get15Days(){
		Calendar  cal   =   Calendar.getInstance();
		//cal.add(Calendar.DATE, -15);
		//String day15=df.format(cal.getTime());
		cal.set(Calendar.DATE, cal.get(Calendar.DATE) - 15);
		String day15=df.format(cal.getTime());
		return day15;
		
	}
	/**
	 * 获取最近三十天时间
	 * @return
	 */
	public static String get30Days(){
		Calendar  cal   =   Calendar.getInstance();
		cal.add(Calendar.DATE, -30);
		String day30=df.format(cal.getTime());
		return day30;
	}
	/**
	 * 获取本月第一天时间
	 * @return
	 */
	public static String getCurrentMonth(){
		Calendar  cal   =   Calendar.getInstance();
		// 获取前月的第一天
		cal.add(Calendar.MONTH, 0);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		String CurrentMonth = df.format(cal.getTime());
		return CurrentMonth;
	}
	/**
	 * 获取上月第一天时间
	 * @return
	 */
	public static String getUpStartMonth(){
		Calendar  cal   =   Calendar.getInstance();
		cal.add(Calendar.MONTH, -1);
        cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天 
        String UpStartMonth = df.format(cal.getTime());
		return UpStartMonth;
		
	}
	/**
	 * 获取上月最后一天时间
	 * @return
	 */
	public static String getUpEndMonth(){
		Calendar  cal   =   Calendar.getInstance();
		cal.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天 
        String UpEndMonth = df.format(cal.getTime());
		return UpEndMonth;
		
	}
	
	public static Map<String, String> getDate(){
		Map<String, String> dateMap=new HashMap<String, String>();
		dateMap.put("currDate",DateUtils.getCurrentDate());
		dateMap.put("yesterday", DateUtils.getYesterday());
		dateMap.put("day7", DateUtils.get7Days());
		dateMap.put("day15", DateUtils.get15Days());
		dateMap.put("day30", DateUtils.get30Days());
		dateMap.put("currMonth", DateUtils.getCurrentMonth());
		dateMap.put("upstartmonth", DateUtils.getUpStartMonth());
		dateMap.put("upendmonth", DateUtils.getUpEndMonth());
		return dateMap;
	} 
	
	public static String getOrderDate(){
		SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return df1.format(new Date());
		
	}
	
	public static void main(String[] args) {
		System.err.println("当前时间："+DateUtils.getCurrentDate());
		System.err.println("昨天时间："+DateUtils.getYesterday());
		System.err.println("7天前时间："+DateUtils.get7Days());
		System.err.println("15天前时间："+DateUtils.get15Days());
		System.err.println("30天前时间："+DateUtils.get30Days());
		System.err.println("本月第一天时间："+DateUtils.getCurrentMonth());
		System.err.println("上月第一天时间："+DateUtils.getUpStartMonth());
		System.err.println("上月最后一天时间："+DateUtils.getUpEndMonth());
		System.err.println(DateUtils.getOrderDate());
	}
}
